//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
Page({
    adLoad() {
        this.setData({
          count: 15
        });
        let that = this;
        let count = 15;
        let i = setInterval(function () {
            console.log(1);
            count--;
            that.setData({
                count: count
            })
            if (count == 0) {
                clearInterval(i);
            }
        },1000)
        that.setData({
            'interval':i
        })
    },
    adError(err) {
        console.log('小程序视频广告加载失败', err)
    },
    adClose() {
        let that = this;
        clearInterval(that.data.interval);
        that.setData({
            showAd:0,
            // count:15
        })
        let count = that.data.count;
        let imgUrl = that.data.nowImg;
        if(count > 0 ){
            wx.showToast({
                title: '观看不足15秒',
            })
        }else{
            //获取相册授权
            wx.getSetting({
                success(res) {
                    if (!res.authSetting['scope.writePhotosAlbum']) {
                        wx.authorize({
                            scope: 'scope.writePhotosAlbum',
                            success() {
                                //这里是用户同意授权后的回调
                                that.saveImgToLocal(imgUrl);
                                wx.showToast({
                                    title: '已保存到相册'
                                })
                            },
                            fail() {//这里是用户拒绝授权后的回调
                                that.setData({
                                    openSettingBtnHidden: false
                                })
                            }
                        })
                    } else {//用户已经授权过了
                        that.saveImgToLocal(imgUrl);
                        wx.showToast({
                            title: '已保存到相册'
                        })
                    }
                }
            })
        }
    },
    data: {
        canIUse:1,
        title:'拼着买更便宜',
        height:0,
        width:0,
        wxapp:{},
        qrcodeList:[],
        recommend:{},
        bannerList:[],
        kf:{},
        showAd:0,
        nowImg:'',
        unit_id:'adunit-1e93867e5ae28126',
        count:15,
        interval:'',
        config:{},
        rewardedVideoAd :null,
    },
    onLoad: function () {
        this.setData({
            height: wx.getSystemInfoSync().windowHeight,
            width: wx.getSystemInfoSync().windowWidth,
            navH: app.globalData.navHeight,
        })
        this.getappInfo();
        if (wx.createRewardedVideoAd) {
            var rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-b87720b43006ff80' })
            this.setData({
                rewardedVideoAd: rewardedVideoAd
            })
            rewardedVideoAd.onLoad(() => {
                console.log('onLoad event emit')
            })
            rewardedVideoAd.onError((err) => {
                console.log('onError event emit', err)
            })
            rewardedVideoAd.onClose((res) => {
                console.log('onClose event emit', res)
            })
            var that = this;
            rewardedVideoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                if (res && res.isEnded) {
                    console.log('播放结束了')
                    let imgUrl = that.data.nowImg;
                    console.log(imgUrl);
                    // 正常播放结束，可以下发游戏奖励
                    wx.getSetting({
                        success(res) {
                            if (!res.authSetting['scope.writePhotosAlbum']) {
                                wx.authorize({
                                    scope: 'scope.writePhotosAlbum',
                                    success() {
                                        //这里是用户同意授权后的回调
                                        that.saveImgToLocal(imgUrl);
                                        wx.showToast({
                                            title: '已保存到相册'
                                        })
                                    },
                                    fail() {//这里是用户拒绝授权后的回调
                                        that.setData({
                                            openSettingBtnHidden: false
                                        })
                                    }
                                })
                            } else {//用户已经授权过了
                                that.saveImgToLocal(imgUrl);
                                wx.showToast({
                                    title: '已保存到相册'
                                })
                            }
                        }
                    })
                } else {
                    console.log('中途退出，播放未完成')
                    // 播放中途退出，不下发游戏奖励
                    wx.showToast({
                        title: '您未看完视频',
                    })
                }
            })
        }
        
        // this.getQrcode();
        // this.getRecommend();
    },
    getappInfo:function(){
        var that = this;
        util.postDataLogin('index/index/getInfo',{},function(res){
            let data = res.data;
            that.setData({
                'wxapp': data.wxapp,
                'recommend': data.recommend,
                'qrcodeList': data.qrcodeList,
                'bannerList': data.bannerList,
                'kf':data.kf,
                'config':data.config
            })
        })
    },
    copyText:function(e){
        var that = this;
        wx.setClipboardData({
            data: that.data.wxapp.wechat_num,
            success:function(res){
                wx.getClipboardData({
                    success: function (res) {
                        wx.showToast({
                            title: '复制成功'
                        })
                    }
                })
            }
        })
    },
    saveImg: function(e){
        let user = wx.getStorageSync('userinfo');
        if(!user.nickname){  //弹出授权按钮
            wx.navigateTo({
                url: '../login/login',
            })
            return false;
        }   
        var that = this;
        var count = 15;
        this.setData({
          'showAd':1,
          'nowImg': e.currentTarget.dataset.imgurl
        });
        
        return false;
        let imgUrl = e.currentTarget.dataset.imgurl;
        //获取相册授权
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            //这里是用户同意授权后的回调
                            that.saveImgToLocal(imgUrl);
                            wx.showToast({
                                title: '已保存到相册'
                            })
                        },
                        fail() {//这里是用户拒绝授权后的回调
                            that.setData({
                                openSettingBtnHidden: false
                            })
                        }
                    })
                } else {//用户已经授权过了
                    that.saveImgToLocal(imgUrl);
                    wx.showToast({
                        title: '已保存到相册'
                    })
                }
            }
        })
    },
    saveImg2:function(e){
        let user = wx.getStorageSync('userinfo');
        if (!user.nickname) {  //弹出授权按钮
            wx.navigateTo({
                url: '../login/login',
            })
            return false;
        }  
        this.setData({
            'nowImg': e.currentTarget.dataset.imgurl
        });
        this.data.rewardedVideoAd.show().catch(() => {
            rewardedVideoAd.load()
            .then(() => rewardedVideoAd.show())
            .catch(err => {
                console.log('激励视频 广告显示失败')
            })
        })
        
    },
    saveImgToLocal: function (imgSrc) {
        let that = this;
        wx.downloadFile({
            url: imgSrc,
            success: function (res) {
                //图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.showToast({
                            title: '已保存到相册',
                            icon: 'success',
                            duration: 2000
                        })
                    },error:function(res){
                        console.log(res)
                    }
                })
            },fail:function(res){
                console.log(res)
            }
        })
    },
    getRecommend:function(e){
        var that = this;
        util.postDataLogin('index/index/getRecommend', {}, function (res) {
            that.setData({
                'recommend': res.data
            })
        })
    },
    getQrcode:function(){
        var that = this;
        util.postDataLogin('index/index/getQrcode', {}, function (res) {
            console.log(res)
            that.setData({
                'qrcodeList': res.data
            })
        })
    }
})
